<template>
    <div style="width:90%;">
        <el-form :model="planForm" :rules="planRules" ref="planForm" label-width="150px">
            <el-button @click="goback()" size="mini" type="primary" style="margin-left:1000px">返回</el-button>
       
        
            <el-form-item label="主题名称：" prop="messageTitle" >
                <el-input
                    v-model="planForm.messageTitle"
                    :disabled="disabledFlag"
                    style="width:360px;"
                    placeholder="请输入主题名称"
                    maxlength="30"
                    show-word-limit
                ></el-input>
            </el-form-item>
            <el-form-item label="主题内容：" prop="messageContent">
                <el-input
                    type="textarea"
                    v-model="planForm.messageContent"
                    :disabled="disabledFlag"
                    placeholder="请输入主题内容"
                    maxlength="200"
                    show-word-limit
                    :autosize="{minRows:4}"
                    style="width:650px;"
                ></el-input>
            </el-form-item>
              <el-form-item label="发布状态：" prop="status">
                <el-radio-group v-model="planForm.status" :disabled="disabledFlag">
                    <el-radio-button label="0">不发布</el-radio-button>
                    <el-radio-button label="1">发布</el-radio-button>
                </el-radio-group>
            </el-form-item>
            <el-form-item >
                <el-button type="primary" @click="submitForm('planForm')" v-if="!disabledFlag">保 存</el-button>
                <el-button @click="resetForm('planForm')">取 消</el-button>
            </el-form-item>
        </el-form>

      
    </div>
</template>
<script>
import {
    addPrize,
    updatePrize,
    queryGenCoupon
} from "@/api/messageNotification";
import {
  queryPromotionList,
  queryPromotionById,
  queryCouponById,
  savePromotion,
  updatePromotionStatus,
  createTPlan
} from "@/api/promotionProgramEdit";
import imageUpload from "@/page/common/Image-upload";
import { queryCouponList } from "@/api/promotionProgramEdit";

export default {
  data() {
    return {
      keepAlive:true,
      logininfo: {},
      orgList: [],
      orgSeq: "",
      loading: true,
      typeOptions: [
              {value: "1",label: '虚拟奖品'}, 
              {value: "2",label: '实物奖品'}],
      lsPlanCouponList: [],
      lsCouponList: [],
      planCouponList: [],
      couponList: [],
      planForm: {
        uuid: "",
        
        messageTitle: "",
        messageContent: "",
         status:"0",
      },
      formCoupon: {
        couponName: ""
      },
      dialogVisibleCoupon: false,
      disabledFlag: false,
      listQuery: {
        current: 1,
        size: 10
      },
      total: 0,
      planRules: {
        
        messageTitle: [{ required: true, message: "请输入主题名称", trigger: "blur" }],
        messageContent: [{ required: true, message: "请输入主题内容", trigger: "blur" }],
        status: [{ required: true, message: "请选择发布状态", trigger: "blur" }]
        
      }
    };
  },
  components: {
        imageUpload
    },
  mounted() {
    this.logininfo = localStorage.getItem("logininfo");
    this.orgList = JSON.parse(this.logininfo).orgList;
    this.orgSeq = this.orgList[0].orgSeq;
    this.planForm.orgSeq = this.orgList[0].orgSeq;

    var v = this.$route.params.edit;
    if (v) {
      // 修改，回显值
      this.planForm.uuid = v.uuid;
      this.planForm.messageTitle = v.messageTitle;
      
      this.planForm.messageContent = v.messageContent;
      
      this.planForm.status = v.status;

 
    } else {
      // 新增，默认值
      this.planForm.uuid = "";
      this.planForm.status = "1";
      this.getCouponList();
    }
  },
  methods: {
    goback() {
      this.$router.go("-1");
    },
  
    getCouponList() {
      let data = {
        size: 100,
        current: 1,
        condition: {
          status: 1,
          couponType: 1,
          keyWord: this.formCoupon.couponName
        },
        ascs: [],
        descs: []
      };
      queryGenCoupon(data).then(res => {
        this.couponList = res.records;
        // 去掉已选的
        for (var i in this.planCouponList) {
          this.removeByValue(
            this.couponList,
            "uuid",
            this.planCouponList[i].uuid
          );
        }
      });
      this.loading = false;
    },

   
    submitForm(formName) {
      if (this.planForm.messageTitle == "" || this.planForm.messageTitle == null) {
        this.$message.error("请输入主题名称");
        return;
  
      }
         if (this.planForm.messageContent == "" || this.planForm.messageContent == null) {
        this.$message.error("请输入主题内容");
        return;
  
      }
        this.keepAlive=false
      if(this.planForm.uuid == ""){

          
      addPrize(this.planForm).then(res => {
        if (res.code == "1") {
          this.$message({
            showClose: true,
            message: "保存成功",
            type: "success"
          });
          this.$router.push("messageNotification");
        } else {
          this.$message({
            showClose: true,
            message: "保存失败",
            type: "success"
          });
        }
      });
      } else {
        updatePrize(this.planForm).then(res => {
        if (res.code == "1") {
          this.$message({
            showClose: true,
            message: "修改成功",
            type: "success"
          });
          this.$router.push("messageNotification");
        } else {
          this.$message({
            showClose: true,
            message: "修改失败",
            type: "success"
          });
        }
      });
      }
    },
    // 数组，属性，属性值
    removeByValue: function(arr, attr, value) {
      if (arr == null) {
        return;
      }
      var index = 0;
      for (var i in arr) {
        if (arr[i][attr] == value) {
          index = i;
          break;
        }
      }
      arr.splice(index, 1);
    },
    handleRemove(file, fileList) {
      let newfileList = [];
      fileList.forEach((item, i) => {
        let data = {
          name: item.name,
          url: item.url
        };
        newfileList.push(data);
      });
      this.fileList = newfileList;
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.$router.push("messageNotification");
    }
  },
  beforeRouteLeave(to, from, next) {
    // 设置下一个路由的 meta
    to.meta.keepAlive = this.keepAlive; // C 跳转到 A 时让 A 不缓存，即刷新
    next();
  },
};
</script>
<style lang="less" scoped>
.el-col-20 {
    border: 1px solid #ebeef5;
    margin-left: 150px;
    width: 60%;
}
.el-input {
    width: 60%;
}
.search-form-inline {
    border: 1px solid #ebeef5;
    line-height: 30px;
    padding: 12px;
    margin-bottom: 5px;
}
thead {
    .el-table-column--selection {
        .cell {
            display: none;
        }
    }
}
</style>